package com.wwwarehouse.usercenter.fragment.register;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.wwwarehouse.common.custom_widget.button.StateButton;
import com.wwwarehouse.common.custom_widget.popupwindow.EasyPopupWindow;
import com.wwwarehouse.common.custom_widget.state_layout.StateLayout;
import com.wwwarehouse.common.tools.ApkTools;
import com.wwwarehouse.common.tools.PopUpUtils;
import com.wwwarehouse.usercenter.R;
import com.wwwarehouse.usercenter.activity.RegisterCardActivity;
import com.wwwarehouse.usercenter.constant.UserCenterConstant;

/**
 * @创建者 chenchao.
 * @创建时间 2017/10/18 17:05
 * @描述 ${TODO}
 */

public class RegisterSuccessFragment extends RegisterCardBaseFragment implements View.OnClickListener, EasyPopupWindow.ChildClickListener {

    private   View            mView;
    private   TextView        mTvName;
    private   StateButton     mBtCheck;
    private   StateButton     mBtKnow;
    protected EasyPopupWindow mEasyPopupWindow;
    private   StateLayout     mStateLayout;
    private   String          mAuditName;
    private   String          mAuditMobile;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_register_success, container, false);
        return mView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
    }

    private void initView() {
        mStateLayout = findView(mView, R.id.sl_layout);
        mTvName = findView(mView, R.id.tv_name);
        mBtCheck = findView(mView, R.id.bt_check);
        mBtKnow = findView(mView, R.id.bt_know);
        mBtCheck.setOnClickListener(this);
        mBtKnow.setOnClickListener(this);
        ((RegisterCardActivity)mActivity).hideBack();
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof RegisterSuccessFragment) {
            mActivity.setTitle(R.string.register_success);
        }
    }

    @Override
    public void requestDatas() {
        Bundle successBundle = getArguments();
        if (successBundle != null && successBundle.getString("auditName") != null && successBundle.getString("auditMobile") != null) {
            mAuditName = successBundle.getString("auditName");
            mAuditMobile = successBundle.getString("auditMobile");
            mTvName.setText(String.format(mActivity.getResources().getString(R.string.register_success_name), mAuditName.trim()));
        }

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.bt_check) {
            mEasyPopupWindow = PopUpUtils.showPopUpBottom(R.layout.invite_has_checked_popupwindow, mActivity, mActivity.findViewById(android.R.id.content), true, RegisterSuccessFragment.this);
        } else if (id == R.id.bt_know) {
            startActivity(UserCenterConstant.PATH_MAIN, null, true);
        } else if (id == R.id.btn_call_phone) {
            if (mAuditMobile != null) {
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + mAuditMobile));
                startActivity(intent);
            }
        } else if (id == R.id.btn_wechat_contact) {
            if (!ApkTools.isWeixinInstall(mActivity)) {
                toast(R.string.wechat_not_installed);
            } else {
                ApkTools.launchWechat(mActivity);
            }
        } else if (id == R.id.btn_cancel) {
            mEasyPopupWindow.dismiss();
        }
    }

    @Override
    public void getChildView(int i, View view) {
        Button call = findView(view, R.id.btn_call_phone);
        Button contact = findView(view, R.id.btn_wechat_contact);
        Button cancel = findView(view, R.id.btn_cancel);
        call.setOnClickListener(this);
        contact.setOnClickListener(this);
        cancel.setOnClickListener(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        ((RegisterCardActivity)mActivity).showBack();
    }
}
